<?php
namespace App\Bots;
use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
class CreateUserBots {

    protected $name;
    public function __construct($name)
    {
        $this->name = $name;
    }

    public function create()
    {
        if (! $this->name) {
            return;
        }
        if( ! $user = $this->getByName($this->name)) {
            $user = $this->createByName();
        }
        return $user;
    }
    public function getByName($name)
    {
        return User::where('name', $name)->where('type', 'douban')->first();
    }

    protected function createByName()
    {
        return User::create([
            'name' => $this->name,
            'email' => Str::uuid() . '@touxiangge.com',
            'password' => Hash::make(Str::uuid()),
            'type' => 'douban'
        ]);
    }
}
